<template>
  <div>
    <el-dialog
      title="切换版本"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-table
        ref="singleTable"
        :data="selections"
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        style="width: 100%;height:40vh"
         v-loading="loading"
      >
      
        <!-- <el-table-column type="selection" width="50"> </el-table-column> -->
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column property="versionNo" label="版本编号" width="120">
        </el-table-column>
        <el-table-column property="description" label="描述"> </el-table-column>
        <el-table-column property="name" label="状态">
          <template slot-scope="scope">
               <span v-if="scope.row.versionNo==value.activeVersion" >激活</span>
           </template>
           </el-table-column>
        <el-table-column property="name" label="操作"> 
           <template slot-scope="scope">
               <el-button v-if="scope.row.versionNo!=value.activeVersion" @click="updateActiveVersion(scope.row)" type="text" size="small"
                  >切换</el-button
                >
           </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    
  },
  watch: {
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    }
  },
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {
       
      },
      rules: {
      },
      currentRow: null,
      selections: [],
      value:{},
      multipleSelection: []
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      this.value = {...value};
      this.loadData(this.value);
    },
    hide() {
      this.visible = false;
      this.form = {
        name: ""
      };
    },
    loadData(value) {
      this.loading = true;
      this.value = value;
      this.getAllWorkflowVersions(value.id).then(() => {
        this.loading = false;
      });
    },
    getAllWorkflowVersions(workflowId) {
      return this.$store
        .dispatch("workflow/getAllWorkflowVersions", {workflowId:workflowId})
        .then(res => {
          console.log(res);
          this.selections = res;
        });
    },
    updateActiveVersion(versionInfo)
    {
       this.loading = true;
       this.$store
        .dispatch("workflow/updateWorkflowActiveVersion", {
          workflowId: versionInfo.workflowId,
          activeVersion: versionInfo.versionNo,
        })
        .then((res) => {
          this.loading = false;
          this.value.activeVersion = versionInfo.versionNo
        });
    },
    commit() {
     
      this.$emit("ok-click", {});
      this.hide();
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    handleSelectionChange(val) {
      debugger
        this.multipleSelection = val;
      }
  }
};
</script>

<style></style>
